package com.pro.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.Logger;

/**
 * ClassName:DateUtils
 * Package:com.pro.uils
 * Description:
 *
 * @Date:2021/11/4 下午 6:47
 * @Author:太宰
 */
public class DateUtil {

    //字符串转时间tranString
    public static Date transString(String myDate) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = format.parse(myDate);
        return date;
    }
    //时间转字符串
    public static String transDate(Date myDate){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(myDate);
        return format;
    }

    //转换int
    public static Integer StrisInt(String str){
        Integer round = (int) Math.round(Double.parseDouble(str));
        return round;
    }
    //字符串转时间
    public static Date tranString(String myDate) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = format.parse(myDate);
        return date;
    }
    //时间转字符串
    public static String tranDate(Date myDate){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(myDate);
        return format;
    };
    //时间转换为周
    public static List<String> getWeekOfMonth(Date date){
        List<String> list = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int week = calendar.get(Calendar.DAY_OF_WEEK);
        if (1 == week){
            calendar.add(Calendar.DAY_OF_MONTH,-1);
        }
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        int day = calendar.get(Calendar.DAY_OF_WEEK);
        calendar.add(Calendar.DATE,calendar.getFirstDayOfWeek()-day);
        list.add(sdf.format(calendar.getTime()));
        for (int i = 1;i<7;i++){
            calendar.add(Calendar.DATE,1);
            list.add(sdf.format(calendar.getTime()));
        }
        return list;
    }
    //获取某个月的天数
    public static int getDaysOfMonth(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }
}
